﻿using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace FutureCorePlugin
{
    /// <summary>
    /// 注意 IsRaycastLocationValid 的判断区域是RectTransform的区域
    /// 如果 polygon Collider编辑出来的区域大于RectTransform ， 必须调节RectTransform的区域。
    /// 1.把按钮的image的RaycastTarget关闭勾选
    /// 2.在子节点创建新的gameObject挂上脚本。
    /// 3.编辑区域即可。
    /// </summary>
    [RequireComponent(typeof(Collider2D))]
    public class UIColliderImage : Image
    {
        private Collider2D _colliderImage = null;
        private Collider2D ColliderImage
        {
            get
            {
                if (_colliderImage == null)
                    _colliderImage = GetComponent<Collider2D>();
                return _colliderImage;
            }
        }

        protected UIColliderImage()
        {
            useLegacyMeshGeneration = true;
        }

        protected override void OnPopulateMesh(VertexHelper vh)
        {
            vh.Clear();
        }

        public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
        {
            return ColliderImage.OverlapPoint(eventCamera.ScreenToWorldPoint(screenPoint));
        }
    }

#if UNITY_EDITOR
    [CustomEditor(typeof(UIColliderImage), true)]
    public class UIColliderImage_Inspector : UnityEditor.Editor
    {
        public override void OnInspectorGUI() { }
    }
#endif
}